package Controler.Eleve;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import JDBC.Eleve_Afficher;
import JDBC.Eleve_Modifier;
import Model.Lieu;
import Model.Famille;
import Model.Listes;
import Model.Eleve;

/**
 * Servlet implementation class Professeur_ModifierController
 */
@WebServlet("/Eleve_ModifierController")
public class Eleve_ModifierController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Eleve_ModifierController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("MODIFICATION CONTROLLER");
		String a = request.getParameter("elselect");
		
		String nom = request.getParameter("nom");
		String prenom = request.getParameter("prenom");
		String sexe = request.getParameter("sexe");
		String date_naiss = request.getParameter("date_naiss");
		String mail = request.getParameter("mail");
		String tel =  request.getParameter("tel");
		String nomP = request.getParameter("nomP");
		String prenomP =  request.getParameter("prenomP");
		String mailP =  request.getParameter("mailP");
		String telP =  request.getParameter("telP");
		String numeroF = request.getParameter("numeroF");
		String rueF = request.getParameter("rueF");
		String cpF = request.getParameter("cpF");
		String villeF = request.getParameter("villeF");
		String compF = request.getParameter("compF");
		
		int nump=0;
		int num=0;
		int codepostal=0;
		
		String JSPURL = "Vues/Professeur/vueGRP1_tableauProf.jsp";
		String erreur;
		
		try
		{
			nump = Integer.parseInt(telP);
		
		}
		catch(NumberFormatException nfe)
		{
			JSPURL = "index.jsp?error=";
			erreur = "Entrez un numero de telephone valide";
		}
		
		try
		{
			num = Integer.parseInt(numeroF);
		
		}
		catch(NumberFormatException nfe)
		{
			JSPURL = "index.jsp?error=";
			erreur = "Entrez un numero de rue valide";
		}
		
		try
		{
			codepostal = Integer.parseInt(cpF);
		
		}
		catch(NumberFormatException nfe)
		{
			JSPURL = "index.jsp?error=";
			erreur = "Entrez un code postal valide";
		}
		
		if(nom.compareTo("") == 0)erreur = "Entrez un nom";
		else if(prenom.compareTo("") == 0)erreur = "Entrez un prenom";
		else if(sexe.compareTo("") == 0)erreur = "Entrez un sexe";
		else if(rueF.compareTo("") == 0)erreur = "Entrez une rue";
		else if(villeF.compareTo("") == 0)erreur = "Entrez une ville";
		else if(date_naiss.compareTo("") == 0)erreur = "Entrez une date de naissance";
		else JSPURL = "Vues/Eleve/vueGRP1_afficher.jsp";

		Eleve e = new Eleve();
		Famille f = new Famille();
		Lieu l = new Lieu();
		
		l.numero = num;
		l.rue = rueF;
		l.cp = codepostal;
		l.ville = villeF;
		l.complement=compF;
		l.idDepartement=1;
		
		e.setNom(nom);
		e.setPrenom(prenom);
		e.setSexe(sexe);
		e.setNaissance(date_naiss);
		e.setEmail(mail);
		e.setTel(tel);
		
		f.setNom(nomP);
		f.setPrenom(prenomP);
		f.setMail(mailP);
		f.setNum(nump);
		
		e.setId_famille(f);
		e.setAdresse_cours(l);
		
		Eleve_Modifier modification = new Eleve_Modifier();
		modification.modifier(e);

		HttpSession session = request.getSession();
		Listes listes = (Listes) session.getAttribute("listes");
		if (listes==null)
		{
			listes = new Listes();
		}
		
		Eleve_Afficher listeEl = new Eleve_Afficher();
		ArrayList<Eleve> mesEleves = listeEl.afficher();
		listes.setMesEleves(mesEleves);
		System.out.println(listes.getMesEleves().size());
		session.setAttribute("listes", listes);
		
		RequestDispatcher rd = request.getRequestDispatcher(JSPURL);
		rd.forward(request, response);
	}

}
